<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @package default
 * @author
 **/
class Product_model extends CI_Model {
	
	function __construct()
	{
		parent::__construct();
	}

	public function get_lists($field='a.*,b.title as typetitle', $cond=NULL ,$sort=' a.id desc', $limit=10, $offset=0){
		$this->db->select($field);

		$this->db->from('product a');
		$this->db->join('producttype b', 'a.typeid=b.id','left');
		if($cond){
			$this->db->where($cond);
			// print_r($cond);
			// $cond = "a.title like '%s%'";
			// $this->db->where($cond);
			// print_r($this->db);
		}
		if($sort){
			$this->db->order_by($sort);
		}		
		if($limit){
			$this->db->limit($limit, $offset);
		}
		
		$rs = $this->db->get();
		return $rs->result();
	}
	
	/**
	 * 获得推荐产品 
	 * Enter description here ...
	 * @param unknown_type $field
	 */
	public function get_product($field="*", $limit=5)
	{
		$this->db->select($field);
		$this->db->where('imagethumb is not null');
		$this->db->limit($limit);
		$this->db->order_by('id desc');
		$rs = $this->db->get("product");
		return $rs->result();
	}
	public function get_products($fields="*",$cond=NULL,$limit=NULL,$sort=NULL){
		$sql = "SELECT {$fields} FROM {$this->db->dbprefix}product p
LEFT JOIN {$this->db->dbprefix}producttype pt ON p.typeid=pt.id";
		if($cond != NULL){
			$sql .= " WHERE ".$cond;
		}		
		if($sort != NULL){
			$sql .= " ORDER BY {$sort}";
		}
		if($limit!=NULL){
			$sql .= " limit ".$limit;
		}
		$rs = $this->db->query($sql);
		return $rs->result();
		
	}
	public function get_ptype($typeid=0){
		$sql = "select * from {$this->db->dbprefix}producttype";
		if($typeid){
			$sql .= " where id=".$typeid;
		}
		$rs = $this->db->query($sql);
		return $rs->result(); 
	}

	

}
/* End of file product_model.php */
/* Location: ./application/model/product_model.php */